package com.maoxs.dao.impl;

import com.maoxs.dao.MyTestDao;
import com.maoxs.pojo.MyTest;
import com.maoxs.util.JdbcDaoImpl;
import com.maoxs.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class MyTestDaoImpl extends JdbcDaoImpl implements MyTestDao {


    @Override
    public MyTest selectOne(Long id) {
        String sql = "select id,name,age,inhere from Test where id=? ";
        return jdbcTemplate.queryForObject(sql, (resultSet, i) -> {
            MyTest myTest = new MyTest();
            myTest.setId(resultSet.getLong("id"));
            myTest.setName(resultSet.getString("name"));
            myTest.setAge(resultSet.getInt("age"));
            myTest.setInhere(resultSet.getBoolean("inhere"));
            return myTest;
        }, id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(MyTest myTest) {
        String sql = "insert into Test (name,age,inhere) values(?,?,?)";
        int i = 1 / 0;
        return jdbcTemplate.update(sql, myTest.getName(), myTest.getAge(), myTest.getInhere());
    }

    @Override
    public int delete(Long id) {
        String sql = "delete from Test where id=?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int update(MyTest myTest) {
        String sql = "update Test set name=?,age=?,inhere=? where id=?";
        return jdbcTemplate.update(sql, myTest.getName(), myTest.getAge(), myTest.getInhere(), myTest.getId());
    }

    /**
     * 分页
     *
     * @param pageCurrent  第几页
     * @param pageSize    每页显示多少条
     * @return
     */
    @Override
    public Page<MyTest> queryForPage(int pageCurrent, int pageSize) {
        String sql = "select * from Test";
        return queryForPage(sql, pageCurrent, pageSize, MyTest.class);
    }
}
